<?php

namespace App\Console\Commands;

use App\Customer;
use App\Lead;
use App\model\RobotConfig;
use App\Service\RobotService;
use Carbon\Carbon;
use Illuminate\Console\Command;

class RobotCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'robot:message {id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '当日简报';
    protected $robotService;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(RobotService $robotService)
    {
        parent::__construct();
        $this->robotService=$robotService;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $id=$this->argument('id');
        $robot=RobotConfig::find($id);
        $time=Carbon::now()->toDateString();
        $project=$robot->project->name ?? '';
        $lead=Lead::where('create_time' ,'<',Carbon::today()->timestamp )
            ->where('create_time' ,'>',Carbon::yesterday()->timestamp )
            ->where('project_id',$robot->project_id)->count();
        $noDisplay=Customer::where('create_time' ,'<',Carbon::today()->timestamp)
            ->where('create_time' ,'>',Carbon::yesterday()->timestamp )
            ->where('project_id',$robot->project_id)->whereIn('status',['高价值客户','潜在客户'])->count();
        $display=Customer::where('create_time' ,'<',Carbon::today()->timestamp)
            ->where('create_time' ,'>',Carbon::yesterday()->timestamp )
            ->where('project_id',$robot->project_id)->where('status','无效线索')->count();
        if($robot){
            $content="当日简报\n项目名：$project\n总线索：{$lead}条\n有意向：{$noDisplay}条\n无意向：{$display}条\n已看现场：0条\n时间:{$time}";
            $this->robotService->send($robot->key,$content);
        }
    }
}
